#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/config.h"
#include "../include/stu.h"
#include "../include/tools.h"
#include "../include/vars.h"

void init() {
    // 打开文件
    if ((fp = fopen(FILENAME, "rb+")) == NULL) {
        if ((fp = fopen(FILENAME, "wb+")) == NULL) {
            pause("Error on opening %s file!", FILENAME);
            exit(EXIT_FAILURE);
        }
    }
    // 获取文件长度
    fileSize = getFileSize(fp);
}

void addStuInfo() {
    Student stu;
    printf("请输入学生ID: ");
    scanf("%d", &stu.id);
    printf("请输入学生姓名: ");
    scanf("%s", stu.name);
    printf("请输入学生成绩: ");
    scanf("%f", &stu.score);

    fseek(fp, 0, SEEK_END); // 定位到文件末尾
    fwrite(&stu, sizeof(Student), 1, fp); // 写入学生信息
    fflush(fp); // 确保写入磁盘

    pause("提示：添加成功！按任意键返回...");
}

void deleteStuInfo() {
    int id, found = 0;
    printf("请输入要删除的学生ID: ");
    scanf("%d", &id);

    Student stu;
    FILE *tempFile = fopen("temp.dat", "wb+");
    if (!tempFile) {
        pause("Error on creating temporary file!");
        return;
    }

    while (fread(&stu, sizeof(Student), 1, fp) == 1) {
        if (stu.id != id) {
            fwrite(&stu, sizeof(Student), 1, tempFile); // 复制到临时文件
        } else {
            found = 1; // 找到并标记
        }
    }

    fclose(fp);
    fclose(tempFile);
    remove(FILENAME); // 删除原文件
    rename("temp.dat", FILENAME); // 重命名临时文件

    if (found) {
        fp = fopen(FILENAME, "rb+"); // 重新打开文件
        pause("提示：删除成功，按任意键返回...");
    } else {
        pause("错误：未找到该学生ID，删除失败！按任意键返回...");
    }
}

void alterStuInfo() {
    int id, found = 0;
    printf("请输入要修改的学生ID: ");
    scanf("%d", &id);

    Student stu;
    while (fread(&stu, sizeof(Student), 1, fp) == 1) {
        if (stu.id == id) {
            found = 1;
            printf("当前学生姓名: %s, 当前成绩: %.2f\n", stu.name, stu.score);
            printf("请输入新的姓名: ");
            scanf("%s", stu.name);
            printf("请输入新的成绩: ");
            scanf("%f", &stu.score);
            fseek(fp, -sizeof(Student), SEEK_CUR); // 定位到当前记录
            fwrite(&stu, sizeof(Student), 1, fp); // 更新记录
            fflush(fp);
            pause("提示：修改成功，按任意键返回...");
            break;
        }
    }

    if (!found) {
        pause("错误：该学生信息不存在，查询失败！按任意键返回...");
    }
}

void findStuByID() {
    int id;
    printf("请输入要查询的学生ID: ");
    scanf("%d", &id);

    Student stu;
    int found = 0;
    while (fread(&stu, sizeof(Student), 1, fp) == 1) {
        if (stu.id == id) {
            printf("学生ID: %d, 姓名: %s, 成绩: %.2f\n", stu.id, stu.name, stu.score);
            found = 1;
            break;
        }
    }

    if (!found) {
        pause("错误：该学生信息不存在，查询失败！按任意键返回...");
    }
}

void findStuByName() {
    char name[50];
    printf("请输入要查询的学生姓名: ");
    scanf("%s", name);

    Student stu;
    int found = 0;
    while (fread(&stu, sizeof(Student), 1, fp) == 1) {
        if (strcmp(stu.name, name) == 0) {
            printf("学生ID: %d, 姓名: %s, 成绩: %.2f\n", stu.id, stu.name, stu.score);
            found = 1;
        }
    }

    if (!found) {
        pause("错误：没有查询到相关记录！按任意键返回...");
    }
}

void findStuByScores() {
    float score;
    printf("请输入要查询的学生成绩: ");
    scanf("%f", &score);

    Student stu;
    int found = 0;
    while (fread(&stu, sizeof(Student), 1, fp) == 1) {
        if (stu.score == score) {
            printf("学生ID: %d, 姓名: %s, 成绩: %.2f\n", stu.id, stu.name, stu.score);
            found = 1;
        }
    }

    if (!found) {
        pause("错误：没有查询到相关记录！按任意键返回...");
    }
}

void showAllStu() {
    Student stu;
    int count = 0;
    printf("所有学生信息：\n");
    while (fread(&stu, sizeof(Student), 1, fp) == 1) {
        printf("学生ID: %d, 姓名: %s, 成绩: %.2f\n", stu.id, stu.name, stu.score);
        count++;
    }
    pause("共有%d条学生信息，按任意键返回...", count);
}
